package com.hu.zhcc.shiro.controller.junM;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.junM.pojo.TInterposeFood;
import com.hu.zhcc.shiro.entity.junM.pojo.TInterposePlain;
import com.hu.zhcc.shiro.entity.junM.pojo.TInterposeScheme;
import com.hu.zhcc.shiro.entity.junM.pojo.TInterposeSprot;
import com.hu.zhcc.shiro.entity.lgx.THealthmgr;
import com.hu.zhcc.shiro.entity.lgx.TMember;
import com.hu.zhcc.shiro.service.junM.InterposeSchemeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/interposeScheme")
public class InterposeSchemeController {

    @Autowired
    private InterposeSchemeService interposeSchemeService;

    @GetMapping("")
    public ResponseEntity<Page<TMember>> queryInterposeClassifyAll(@RequestParam(required = false) String name,
                                                                   int offset, int limit){
        Map<String, Object> paremeters = new HashMap<String, Object>();
        if (!StringUtils.isBlank(name)) {
            paremeters.put("name", name);
        }
        Page<TMember> result = this.interposeSchemeService.queryMemberByStatusAll(paremeters,offset,limit);
        System.out.println(result);
        List<TMember> voList = new ArrayList<TMember>();
        if(result.getRows()==null){
            return ResponseEntity.ok(new Page<TMember>(result.getTotal(), voList));
        }

        for(TMember dto : result.getRows()) {
            TMember vo = new TMember();
            BeanUtils.copyProperties(dto, vo);
            voList.add(vo);
        }
        return ResponseEntity.ok(new Page<TMember>(result.getTotal(), voList));
    }

    @GetMapping("/{id}")
    public ResponseEntity<TMember> getInterposeTypeById(@PathVariable("id") int id){

        TMember dto = this.interposeSchemeService.getId(id);
        if(dto == null) {
            return ResponseEntity.notFound().build();
        }
        TMember vo = new TMember();
        BeanUtils.copyProperties(dto, vo);
        return ResponseEntity.ok(vo);

    }

    @PostMapping("/{id}/{scheme}/{eat}/{sprot}/{plain}")
    public void addInterposeScheme(@PathVariable("id") int id,
                                   @PathVariable("scheme") String scheme,
                                   @PathVariable("eat") String eat,
                                   @PathVariable("sprot") String sprot,
                                   @PathVariable("plain") String plain){
        System.out.println(1);
        System.out.println(id+","+scheme+","+eat+","+sprot+","+plain);
        TInterposeScheme tInterposeScheme = JSON.parseObject(scheme, TInterposeScheme.class);
        TInterposeFood tInterposeFood = JSON.parseObject(eat, TInterposeFood.class);
        TInterposeSprot tInterposeSprot = JSON.parseObject(sprot, TInterposeSprot.class);
        TInterposePlain tInterposePlain = JSON.parseObject(plain, TInterposePlain.class);

        this.interposeSchemeService.addInterposeScheme(id,tInterposeScheme,tInterposeFood,tInterposeSprot,tInterposePlain);

    }

    @GetMapping("/queryHealthmgrAll")
    public List<THealthmgr> queryHealthmgrAll(){
        return this.interposeSchemeService.queryHealthmgrAll();
    }


}
